package edu.stanford.prpl.app.prpltalk.android;

import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.Iterator;
import java.util.Arrays;
import java.lang.Integer;


import edu.stanford.prpl.app.prpltalk.PRPLTalk;
import edu.stanford.prpl.app.prpltalk.PRPLTalk.BuddyInfo;
import edu.stanford.prpl.app.prpltalk.android.R.id;
import edu.stanford.prpl.app.prpltalk.android.R.layout;
import edu.stanford.prpl.app.prpltalk.android.R.string;
import edu.stanford.prpl.api.Identity;
import edu.stanford.prpl.api.PRPLAppClient;
import edu.stanford.prpl.common.util.URIUtils;
import edu.stanford.prpl.common.PRPLProperties;

import android.util.Log;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ImageButton;
import android.widget.TextView;
import android.graphics.Color;
import android.database.Cursor;
import android.widget.SimpleCursorAdapter;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnCancelListener;

public class PrPlTalkActivity extends Activity 
{
	/* Pretty important objects */
	public static PRPLTalk prplTalk;
	public static PRPLAppClient prplAppClient;

    private static final int ACTIVITY_DISPLAY_CONTACTS=1;	
	
    private static final String TAG = "MyActivity";
    private static String defaultConfigFilePath = "/sdcard/prpl.id2.properties";
    
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_page);
        setTitle("PrPlTalk Login");
				
        TextView specifyConfigLabel = (TextView) findViewById(R.id.specifyConfigLabel);
        EditText specifyConfigEdit  = (EditText) findViewById(R.id.specifyConfigEdit);      
        Button loginButton   	   = (Button) findViewById(R.id.loginBtn);		

        specifyConfigEdit.setText(defaultConfigFilePath);
		
        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) 
            {
                EditText specifyConfigEdit  = (EditText) findViewById(R.id.specifyConfigEdit);
            	String configFilePath = specifyConfigEdit.getText().toString().trim();
            
            	if ((configFilePath != null) && (configFilePath.length() > 0))
            	{
            		try 
            		{
            			Log.d(TAG, "Going to load PrPl Properties File...");
            			PRPLProperties.loadProperties(configFilePath);
            			defaultConfigFilePath = configFilePath;          			

            			String myEmail = PRPLProperties.getProperty("prpl.user.key");
            			String myName = PRPLProperties.getProperty("prpl.user.name");
            			
            			Log.d(TAG, "PrPlUserKey: "+myEmail);
            			Log.d(TAG, "PrPlUserName: "+myName);
            			Log.d(TAG, "PrPlDirUrl: "+PRPLProperties.getProperty("prpl.directory.url"));

            			PrPlTalkActivity.prplTalk = new PRPLTalk();
            			PrPlTalkActivity.prplAppClient = prplTalk.getPRPLAppClient();
            			
            			ContactManager.getInstance().setMyContact(myName, myEmail);

            			// Problem here?
            			// Log.d(TAG, "NUMBER OF COMMUNICATION RESOURCES # " + PrPlTalkActivity.prplTalk.getAllCommunicationResources().length);
            	        // PrPlTalkActivity.prplTalk.synchWithAll();            			
            			
            			// Go to "Contacts" Page now
                        Intent i = new Intent();
                        i.setClassName("edu.stanford.prpl.app.prpltalk.android", "edu.stanford.prpl.app.prpltalk.android.ContactsPage");
                        // i.putExtra(EditContact.KEY_CONTACT_TYPE, EditContact.ACTIVITY_EDIT_PERSONAL_CONTACT);
                        startActivityForResult(i, PrPlTalkActivity.ACTIVITY_DISPLAY_CONTACTS);           			
            		} 
            		catch (Exception e) 
            		{
            			Log.d(TAG, e.toString());
            			Log.d(TAG, "ERROR LOADING PRPL PROPERTIES FILE!!!");
                		errorInfoBox("Error loading config file '"+configFilePath+"'. Please check that you have a valid config file path.");
                		return;
            		}	
            	}
            	else
            	{	
            		errorInfoBox("No file path to a config file specified. Please specify a config file to use.");
            		return;
            	}
            }
        });
        
    }

	@Override
	protected void onResume() 
	{    
		Log.d(TAG, "On Resume() called!");
		
		super.onResume();
	}

	
	public void errorInfoBox(String errorMsg)
	{
	    new AlertDialog.Builder(this)
	      .setTitle("Error")
	      .setMessage(errorMsg)
	      .setPositiveButton("OK", new OnClickListener() 
      		{
				
				public void onClick(DialogInterface dialog, int which) 
				{ return; }
      		})
	      .setOnCancelListener(new OnCancelListener() 
	      	{
	    	  	
	    	  	public void onCancel(DialogInterface dialog) 
	    	  	{ return; }
	      	})
	      .show();
	}    

	
}